<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="theme-color" content="#0078E7"><meta name="author" content="CC康纳百川"><meta name="copyright" content="CC康纳百川"><meta name="generator" content="Hexo 5.2.0"><meta name="theme" content="hexo-theme-yun"><title>Github Actions为什么这么香 | CC康纳百川</title><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@900&amp;display=swap" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/star-markdown-css@0.1.21/dist/yun/yun-markdown.min.css"><script src="//at.alicdn.com/t/font_1140697_ed8vp4atwoj.js" async></script><script src="https://cdn.jsdelivr.net/npm/scrollreveal/dist/scrollreveal.min.js" defer></script><script>document.addEventListener("DOMContentLoaded", () => {
  [".post-card",".post-content img"].forEach((target)=> {
    ScrollReveal().reveal(target);
  })
});
</script><link rel="shortcut icon" type="image/svg+xml" href="/yun/yun.svg"><link rel="mask-icon" href="/yun/yun.svg" color="#0078E7"><link rel="alternate icon" href="/yun.ico"><link rel="preload" href="/yun/css/hexo-theme-yun.css" as="style"><link rel="preload" href="/yun/js/utils.js" as="script"><link rel="preload" href="/yun/js/hexo-theme-yun.js" as="script"><link rel="prefetch" href="/yun/js/sidebar.js" as="script"><link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin><link rel="stylesheet" href="/yun/css/hexo-theme-yun.css"><script id="yun-config">
    const Yun = window.Yun || {};
    window.CONFIG = {"hostname":"ccknbc.github.io","root":"/yun/","title":"云游君的小站","version":"1.2.0","mode":"time","copycode":true,"anonymous_image":"https://cdn.jsdelivr.net/gh/YunYouJun/cdn/img/avatar/none.jpg","say":{"api":"https://v1.hitokoto.cn","hitokoto":true},"local_search":{"path":"/yun/search.xml"},"fireworks":{"colors":["102, 167, 221","62, 131, 225","33, 78, 194"]}};
  </script><script>(function(){
  var bp = document.createElement('script');
  var curProtocol = window.location.protocol.split(':')[0];
  if (curProtocol === 'https') {
    bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
  }
  else {
    bp.src = 'http://push.zhanzhang.baidu.com/push.js';
  }
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(bp, s);
})();</script><meta name="description" content="Github Actions为什么这么香,我为什么专门建了一个组织ccknbc-actions来跑actions并储存博客源码，或者其他只是单纯来跑actions的项目，一开始就单纯只是想分类，forked一堆，主账号自己的项目却混在其中，最新的反而是天天运行actions的（排在前面） 而对大多数博主来说，都是把自己的源码（包含源文章）仓库私有起来，而一个普通账号每个月私有仓库运行时间为2000">
<meta property="og:type" content="article">
<meta property="og:title" content="Github Actions为什么这么香">
<meta property="og:url" content="https://ccknbc.github.io/yun/posts/9/">
<meta property="og:site_name" content="CC康纳百川">
<meta property="og:description" content="Github Actions为什么这么香,我为什么专门建了一个组织ccknbc-actions来跑actions并储存博客源码，或者其他只是单纯来跑actions的项目，一开始就单纯只是想分类，forked一堆，主账号自己的项目却混在其中，最新的反而是天天运行actions的（排在前面） 而对大多数博主来说，都是把自己的源码（包含源文章）仓库私有起来，而一个普通账号每个月私有仓库运行时间为2000">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/ccknbc-backup/photos/blog/2020-10-06~11:57:17.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/ccknbc-backup/photos/blog/2020-10-06~12:03:33.png">
<meta property="article:published_time" content="2020-10-06T02:51:58.000Z">
<meta property="article:modified_time" content="2020-10-06T04:00:00.000Z">
<meta property="article:author" content="CC康纳百川">
<meta property="article:tag" content="Github Actions">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/ccknbc-backup/photos/blog/2020-10-06~11:57:17.png"><script src="/yun/js/ui/mode.js"></script></head><body><script defer src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script defer src="/yun/js/ui/fireworks.js"></script><canvas class="fireworks"></canvas><div class="container"><a class="sidebar-toggle hty-icon-button" id="menu-btn"><div class="hamburger hamburger--spin" type="button"><span class="hamburger-box"><span class="hamburger-inner"></span></span></div></a><div class="sidebar-toggle sidebar-overlay"></div><aside class="sidebar"><script src="/yun/js/sidebar.js"></script><ul class="sidebar-nav"><li class="sidebar-nav-item sidebar-nav-toc hty-icon-button sidebar-nav-active" data-target="post-toc-wrap" title="文章目录"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-list-ordered"></use></svg></li><li class="sidebar-nav-item sidebar-nav-overview hty-icon-button" data-target="site-overview-wrap" title="站点概览"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-passport-line"></use></svg></li></ul><div class="sidebar-panel" id="site-overview-wrap"><div class="site-info mickey-mouse"><a class="site-author-avatar" href="/yun/about/" title="CC康纳百川"><img width="96" loading="lazy" src="/yun/Yun.png" alt="CC康纳百川"><span class="site-author-status" title="永远相信美好的事情即将发生">😊</span></a><div class="site-author-name"><a href="/yun/about/">CC康纳百川</a></div><a class="site-name" href="/yun/about/site">CC康纳百川</a><sub class="site-subtitle">Hello Word</sub><div class="site-desciption">一个无趣的人</div></div><nav class="site-state"><a class="site-state-item hty-icon-button icon-home" href="/yun/" title="首页"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-home-4-line"></use></svg></span></a><div class="site-state-item"><a href="/yun/archives/" title="归档"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-archive-line"></use></svg></span><span class="site-state-item-count">10</span></a></div><div class="site-state-item"><a href="/yun/categories/" title="分类"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-2-line"></use></svg></span><span class="site-state-item-count">16</span></a></div><div class="site-state-item"><a href="/yun/tags/" title="标签"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="site-state-item-count">18</span></a></div><a class="site-state-item hty-icon-button" target="_blank" rel="noopener" href="https://yun.yunyoujun.cn" title="文档"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-settings-line"></use></svg></span></a></nav><hr style="margin-bottom:0.5rem"><div class="links-of-author"><a class="links-of-author-item hty-icon-button" rel="noopener" href="/atom.xml" title="RSS" target="_blank" style="color:orange"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-rss-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://shang.qq.com/wpa/qunwpa?idkey=c929e704022704d8cced9ec355d44a3fa7ad34aea12cef1de03d75d3d7d5b059" title="QQ 群" target="_blank" style="color:#12B7F5"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-qq-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://github.com/YunYouJun" title="GitHub" target="_blank" style="color:#6e5494"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-github-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://weibo.com/jizhideyunyoujun" title="微博" target="_blank" style="color:#E6162D"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-weibo-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://www.douban.com/people/yunyoujun/" title="豆瓣" target="_blank" style="color:#007722"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-douban-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://music.163.com/#/user/home?id=247102977" title="网易云音乐" target="_blank" style="color:#C20C0C"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-netease-cloud-music-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://www.zhihu.com/people/yunyoujun/" title="知乎" target="_blank" style="color:#0084FF"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-zhihu-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://space.bilibili.com/1579790" title="哔哩哔哩" target="_blank" style="color:#FF8EB3"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-bilibili-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://twitter.com/YunYouJun" title="Twitter" target="_blank" style="color:#1da1f2"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-twitter-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://t.me/elpsycn" title="Telegram Channel" target="_blank" style="color:#0088CC"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-telegram-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="mailto:me@yunyoujun.cn" title="E-Mail" target="_blank" style="color:#8E71C1"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-mail-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://travellings.now.sh/" title="Travelling" target="_blank" style="color:var(--hty-text-color)"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-send-plane-2-line"></use></svg></a></div><hr style="margin:0.5rem 1rem"><div class="links"><a class="links-item hty-icon-button" href="/yun/links/" title="我的小伙伴们" style="color:dodgerblue"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-genderless-line"></use></svg></a></div><br><a class="links-item hty-icon-button" id="toggle-mode-btn" href="javascript:;" title="Mode" style="color: #f1cb64"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-contrast-2-line"></use></svg></a></div><div class="sidebar-panel sidebar-panel-active" id="post-toc-wrap"><div class="post-toc"><div class="post-toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#GitHub-Actions-%E6%8C%87%E5%8D%97"><span class="toc-number">1.</span> <span class="toc-text">GitHub Actions 指南</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="toc-number">2.</span> <span class="toc-text">基本概念</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#steps"><span class="toc-number">3.</span> <span class="toc-text">steps</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#action"><span class="toc-number">4.</span> <span class="toc-text">action</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#checkout-action"><span class="toc-number">5.</span> <span class="toc-text">checkout action</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%95%E7%94%A8-action"><span class="toc-number">6.</span> <span class="toc-text">引用 action</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%95%E7%94%A8%E5%85%AC%E6%9C%89%E5%BA%93%E4%B8%AD%E7%9A%84-action"><span class="toc-number">7.</span> <span class="toc-text">引用公有库中的 action</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%95%E7%94%A8%E5%90%8C%E4%B8%80%E4%B8%AA%E5%BA%93%E4%B8%AD%E7%9A%84-action"><span class="toc-number">8.</span> <span class="toc-text">引用同一个库中的 action</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%95%E7%94%A8-Docker-Hub-%E4%B8%8A%E7%9A%84-container"><span class="toc-number">9.</span> <span class="toc-text">引用 Docker Hub 上的 container</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9E%84%E5%BB%BA-actions"><span class="toc-number">10.</span> <span class="toc-text">构建 actions</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#env"><span class="toc-number">11.</span> <span class="toc-text">env</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#if-amp-context"><span class="toc-number">12.</span> <span class="toc-text">if &amp; context</span></a></li></ol></div></div></div></aside><main class="sidebar-translate" id="content"><div id="post"><article class="post-block" itemscope itemtype="https://schema.org/Article"><link itemprop="mainEntityOfPage" href="https://ccknbc.github.io/yun/yun/posts/9/"><span hidden itemprop="author" itemscope itemtype="https://schema.org/Person"><meta itemprop="name" content="CC康纳百川"><meta itemprop="description"></span><span hidden itemprop="publisher" itemscope itemtype="https://schema.org/Organization"><meta itemprop="name" content="CC康纳百川"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">Github Actions为什么这么香<a class="post-edit-link" href="https://github.com/ccknbc-actions/blog-yun/blob/master/source/_posts/技术/Github Actions为什么这么香.md" target="_blank" title="编辑" rel="noopener"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-edit-line"></use></svg></a></h1><div class="post-meta"><div class="post-time" style="display:block"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-calendar-line"></use></svg></span> <span class="post-meta-icon-text">发表于</span> <time title="创建时间：2020-10-06 10:51:58" itemprop="dateCreated datePublished" datetime="2020-10-06T10:51:58+08:00">2020-10-06</time></div><span class="post-count"><span class="post-symbolcount"><span class="post-meta-item-icon" title="本文字数"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-file-word-line"></use></svg></span> <span title="本文字数">3.9k</span><span class="post-meta-divider">-</span><span class="post-meta-item-icon" title="阅读时长"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-timer-line"></use></svg></span> <span title="阅读时长">17m</span></span></span><span class="post-busuanzi"><span class="post-meta-divider">-</span><span class="post-meta-item-icon" title="阅读次数"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-eye-line"></use></svg> <span id="busuanzi_value_page_pv"></span></span></span><div class="post-classify"><span class="post-category"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-line"></use></svg></span> <span itemprop="about" itemscope itemtype="https://schema.org/Thing"><a class="category" href="/yun/categories/Github-Actions/" style="--text-color:var(--hty-text-color)" itemprop="url" rel="index"><span itemprop="text">Github Actions</span></a></span></span><span class="post-tag"><span class="post-meta-divider">-</span><a class="tag" href="/yun/tags/Github-Actions/" style="--text-color:var(--hty-text-color)"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="tag-name">Github Actions</span></a></span></div></div></header><section class="post-body" itemprop="articleBody"><div class="post-content markdown-body" style="--smc-primary:#0078E7;"><p><code>Github Actions</code>为什么这么香,我为什么专门建了一个组织<code>ccknbc-actions</code>来跑actions并储存博客源码，或者其他只是单纯来跑actions的项目，一开始就单纯只是想分类，forked一堆，主账号自己的项目却混在其中，最新的反而是天天运行actions的（排在前面）</p>
<p>而对大多数博主来说，都是把自己的源码（包含源文章）仓库私有起来，而一个普通账号每个月私有仓库运行时间为2000分钟，确实，是完全够用的，对于我们这种月更博主来说一个月100分钟而已（<del>好吧我就是懒</del>）；不过作为白嫖党我还是公开了仓库（这样就不限制运行时间了），但如果你选择了定时执行某些项目，注意间隔最小为<code>5分钟</code>，而触发方式也是多种多样的，但最让人欣喜的是手动触发方式终于完成了技术革命，不再监控star动作了，而是只有项目所有者可执行，这样就不会再运行记录里出现一堆无关记录影响查看（虽然之前判断仓库所有者也是OK，但是强迫症不接受两秒运行跳过无关记录）</p>
<p>而且由于分配的临时机性能还不错，速度还是很可观的，并且不受长城防火墙的限制，可以很方便的做一些其他事情，这里就不细说，回到标题，它为什么这么香呢？</p>
<p>可能在工作流文件书写上没其他第三方简单，用过<code>Travis CI</code>或者<code>Gitlab CI</code>的都应该有体会，但自家的经过这么久的发展，用起来还是比较舒服的，而且用户编写的actions可以直接拿来用发布到市场，这样有些功能的实现就不用再自己想半天去实现，几行代码就搞定，用HEXO写博客的都知道Gitee托管静态网页要更新的话就得手动点一下更新才行，而actions就可以帮我们去点一下以实现同步全自动更新，多平台部署</p>
<p>Github本身自带的模板是OK的，但毕竟很多新手完全不懂，但其实上手一段时间就明白了每一步在做什么为什么要这样写，官方文档的例子虽然很简单但确实是包含了常用的一些步骤，再加上很多人分享，直接拿来用就行，毕竟搜索引擎不能摆在那不用，爬几篇文章学习一下就会了，这里我以部署HEXO博客为例来说一下</p>
<p>首先我们得明白，要让Github能在你git push上去后能自动部署博客，你得知道博客的源文件是必须要的，但备份哪些呢，下图只是以本站做一个示例，框中的是必须要有的（如果你不打算修改主题源码其实也不用备份，但毕竟可以节约时间，第一次备份前记得在主题文件夹删掉<code>.git</code>隐藏文件夹，不然推上去是空的会导致运行失败），其他根据个人需求，，配一个证书或者README等，但你还在疑惑；我没有.github文件夹啊，没错这个就是我们自己新建的文件夹，在博客根目录依次建立<code>.github/workflows/*.yml</code>文件</p>
<p><img src="https://cdn.jsdelivr.net/gh/ccknbc-backup/photos/blog/2020-10-06~11:57:17.png" alt="备份源文件" loading="lazy" loading="lazy"></p>
<p>就像下图这样，工作流文件.yml你可以随便命名啦，然后我们在工作流文件中添加如下内容</p>
<p><img src="https://cdn.jsdelivr.net/gh/ccknbc-backup/photos/blog/2020-10-06~12:03:33.png" alt="目录" loading="lazy" loading="lazy"></p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">name:</span> <span class="string">Update</span> <span class="string">Blog</span> <span class="string">Butterfly</span> <span class="string">Site</span></span><br><span class="line"></span><br><span class="line"><span class="attr">on:</span> </span><br><span class="line">  <span class="attr">push:</span></span><br><span class="line">    <span class="attr">branches:</span> </span><br><span class="line">      <span class="bullet">-</span> <span class="string">master</span></span><br><span class="line">  <span class="attr">workflow_dispatch:</span></span><br><span class="line">    <span class="attr">inputs:</span></span><br><span class="line">      <span class="attr">name:</span></span><br><span class="line">        <span class="attr">description:</span> <span class="string">&#x27;手动触发&#x27;</span></span><br><span class="line">        <span class="attr">required:</span> <span class="literal">false</span></span><br><span class="line"></span><br><span class="line"><span class="attr">jobs:</span></span><br><span class="line">  <span class="attr">deploy:</span></span><br><span class="line">    <span class="attr">runs-on:</span> <span class="string">ubuntu-latest</span></span><br><span class="line"></span><br><span class="line">    <span class="attr">steps:</span></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Checkout</span></span><br><span class="line">      <span class="attr">uses:</span> <span class="string">actions/checkout@v2</span></span><br><span class="line">      <span class="attr">with:</span></span><br><span class="line">        <span class="attr">ref:</span> <span class="string">master</span></span><br><span class="line"></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Setup</span> <span class="string">Node</span></span><br><span class="line">      <span class="attr">uses:</span> <span class="string">actions/setup-node@v1</span></span><br><span class="line">      <span class="attr">with:</span></span><br><span class="line">        <span class="attr">node-version:</span> <span class="string">&quot;12.x&quot;</span></span><br><span class="line">        </span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Install</span> <span class="string">HEXO</span> <span class="string">CI</span></span><br><span class="line">      <span class="attr">run:</span> <span class="string">|</span></span><br><span class="line">        <span class="string">npm</span> <span class="string">install</span> <span class="string">hexo-cli</span> <span class="string">-g</span></span><br><span class="line"></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Catch</span></span><br><span class="line">      <span class="attr">uses:</span> <span class="string">actions/cache@v2</span></span><br><span class="line">      <span class="attr">id:</span> <span class="string">cache-dependencies</span></span><br><span class="line">      <span class="attr">with:</span></span><br><span class="line">        <span class="attr">path:</span> <span class="string">node_modules</span></span><br><span class="line">        <span class="attr">key:</span> <span class="string">$&#123;&#123;runner.OS&#125;&#125;-$&#123;&#123;hashFiles(&#x27;**/package-lock.json&#x27;)&#125;&#125;</span></span><br><span class="line"></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Install</span> <span class="string">Dependencies</span></span><br><span class="line">      <span class="attr">if:</span> <span class="string">steps.cache-dependencies.outputs.cache-hit</span> <span class="type">!=</span> <span class="string">&#x27;true&#x27;</span></span><br><span class="line">      <span class="attr">run:</span> <span class="string">|</span></span><br><span class="line">        <span class="string">npm</span> <span class="string">install</span></span><br><span class="line"></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Generate</span></span><br><span class="line">      <span class="attr">run:</span> <span class="string">|</span></span><br><span class="line">        <span class="string">rm</span> <span class="string">-rf</span> <span class="string">./public</span></span><br><span class="line">        <span class="string">rm</span> <span class="string">-rf</span> <span class="string">./db.json</span></span><br><span class="line">        <span class="string">rm</span> <span class="string">-rf</span> <span class="string">./.deploy_git</span></span><br><span class="line">        <span class="string">hexo</span> <span class="string">-v</span></span><br><span class="line">        <span class="string">hexo</span> <span class="string">clean</span></span><br><span class="line">        <span class="string">hexo</span> <span class="string">generate</span></span><br><span class="line">        <span class="string">gulp</span></span><br><span class="line">    </span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Deploy</span></span><br><span class="line">      <span class="attr">run:</span> <span class="string">|</span></span><br><span class="line">        <span class="string">cd</span> <span class="string">./public</span></span><br><span class="line">        <span class="string">git</span> <span class="string">init</span></span><br><span class="line">        <span class="string">git</span> <span class="string">config</span> <span class="string">--global</span> <span class="string">user.name</span> <span class="string">&quot;ccknbc&quot;</span></span><br><span class="line">        <span class="string">git</span> <span class="string">config</span> <span class="string">--global</span> <span class="string">user.email</span> <span class="string">&#x27;$<span class="template-variable">&#123;&#123; secrets.GIT_EMAIL &#125;&#125;</span>&#x27;</span></span><br><span class="line">        <span class="string">git</span> <span class="string">add</span> <span class="string">-A</span></span><br><span class="line">        <span class="string">git</span> <span class="string">commit</span> <span class="string">-m</span> <span class="string">&quot;Updated By Github Actions&quot;</span></span><br><span class="line">        <span class="string">git</span> <span class="string">push</span> <span class="string">--force</span> <span class="string">--quiet</span> <span class="string">&quot;https://ccknbc:$<span class="template-variable">&#123;&#123; secrets.GH_TOKEN &#125;&#125;</span>@github.com/ccknbc/butterfly.git&quot;</span> <span class="string">master:master</span></span><br><span class="line">        <span class="string">git</span> <span class="string">push</span> <span class="string">--force</span> <span class="string">--quiet</span> <span class="string">&quot;https://ccknbc:$<span class="template-variable">&#123;&#123; secrets.GT_TOKEN &#125;&#125;</span>@gitee.com/ccknbc/butterfly.git&quot;</span> <span class="string">master:master</span></span><br><span class="line">        <span class="string">git</span> <span class="string">push</span> <span class="string">--force</span> <span class="string">--quiet</span> <span class="string">&quot;https://$<span class="template-variable">&#123;&#123; secrets.CD_USER &#125;&#125;</span>:$<span class="template-variable">&#123;&#123; secrets.CD_TOKEN &#125;&#125;</span>@e.coding.net/ccknbc/blog/butterfly.git&quot;</span> <span class="string">master:master</span></span><br><span class="line"></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Build</span> <span class="string">Gitee</span> <span class="string">Pages</span></span><br><span class="line">      <span class="attr">uses:</span> <span class="string">yanglbme/gitee-pages-action@master</span></span><br><span class="line">      <span class="attr">with:</span></span><br><span class="line">          <span class="attr">gitee-username:</span> <span class="string">ccknbc</span></span><br><span class="line">          <span class="attr">gitee-password:</span> <span class="string">$&#123;&#123;</span> <span class="string">secrets.GITEE_PASSWORD</span> <span class="string">&#125;&#125;</span></span><br><span class="line">          <span class="attr">gitee-repo:</span> <span class="string">ccknbc/butterfly</span></span><br></pre></td></tr></table></figure>

<p>你可能有所疑惑但又看得懂一些命令，别急，请接着往下看</p>
<h2 id="GitHub-Actions-指南"><a href="#GitHub-Actions-指南" class="headerlink" title="GitHub Actions 指南"></a>GitHub Actions 指南</h2><p><code>GitHub Actions</code>使你可以直接在你的<code>GitHub</code>库中创建自定义的工作流，工作流指的就是自动化的流程，比如构建、测试、打包、发布、部署等等，也就是说你可以直接进行 <code>CI</code>（持续集成）和 <code>CD</code>（持续部署）</p>
<h2 id="基本概念"><a href="#基本概念" class="headerlink" title="基本概念"></a>基本概念</h2><ol>
<li><p><code>workflow</code>: 一个 <code>workflow</code> 工作流就是一个完整的过程，每个<code>workflow</code> 包含一组 <code>jobs</code>任务</p>
</li>
<li><p><code>job : jobs</code>任务包含一个或多个<code>job</code> ，每个 <code>job</code>包含一系列的 <code>steps</code> 步骤</p>
</li>
<li><p><code>step</code> : 每个 <code>step</code> 步骤可以执行指令或者使用一个 <code>action</code> 动作</p>
</li>
<li><p><code>action</code> : 每个 <code>action</code> 动作就是一个通用的基本单元</p>
</li>
</ol>
<p>说明：</p>
<ol>
<li><p>最外层的 <code>name</code> 指定了 <code>workflow</code>的名称</p>
</li>
<li><p><code>on</code> 声明了一旦发生了 <code>push</code> 操作就会触发这个 <code>workflow</code></p>
</li>
<li><p><code>jobs</code>定义了任务集，其中可以有一个或多个<code>job</code> 任务，示例中只有一个</p>
</li>
<li><p><code>runs-on</code> 声明了运行的环境</p>
</li>
<li><p><code>steps</code>定义需要执行哪些步骤</p>
</li>
<li><p>每个 <code>step</code>可以定义自己的 <code>name</code>和<code>id</code>，通过 <code>uses</code> 可以声明使用一个具体的 <code>action</code>，通过<code>run</code> 声明需要执行哪些指令。</p>
</li>
<li><p><code>$&#123;&#123;&#125;&#125;`可以使用上下文参数

`on` 声明了何时触发 `workflow`  ，它可以是：

1. 一个或多个`GitHub` 事件，比如 `push` 了一个 `commit` 、创建了一个`issue` ，产生了一次`pull request` 等等，示例：

<figure class="highlight yml"><table><tr><td class="code"><pre><span class="line"><span class="string">on:[push,pull_request]</span></span><br></pre></td></tr></table></figure>

2. 预定的时间，示例（每天零点零分触发，不过因为时差关系，是北京时间上午8点）：

<figure class="highlight yml"><table><tr><td class="code"><pre><span class="line"><span class="attr">on:</span></span><br><span class="line">  <span class="attr">schedule:</span></span><br><span class="line">    <span class="string">-cron:&#x27;0</span> <span class="number">0</span> <span class="string">*</span> <span class="string">*</span> <span class="string">*&#x27;</span></span><br></pre></td></tr></table></figure>

3. 某个外部事件。所谓外部事件触发，简而言之就是你可以通过`REST API`向 `GitHub` 发送请求去触发，具体请查阅官方文档: `repository-dispatch-event`

配置多个事件，示例：

<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">on:</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Trigger the workflow on push or pull request,</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># but only for the master branch</span></span><br><span class="line"></span><br><span class="line"><span class="attr">push:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">branches:</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="string">master</span></span><br><span class="line"></span><br><span class="line"><span class="attr">pull_request:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">branches:</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="string">master</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Also trigger on page_build, as well as release created events</span></span><br><span class="line"></span><br><span class="line"><span class="attr">page_build:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">release:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">types:</span> <span class="comment"># This configuration does not affect the page_build event above</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="string">created</span></span><br></pre></td></tr></table></figure>

详细文档请参考: 触发事件

## jobs

`jobs`可以包含一个或多个 `job` ，如:

<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">jobs:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">my_first_job:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">name:</span> <span class="string">My</span> <span class="string">first</span> <span class="string">job</span></span><br><span class="line"></span><br><span class="line"><span class="attr">my_second_job:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">name:</span> <span class="string">My</span> <span class="string">second</span> <span class="string">job</span></span><br></pre></td></tr></table></figure>

如果多个`job` 之间存在依赖关系，那么你可能需要使用 `needs` :

<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">jobs:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">job1:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">job2:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">needs:</span> <span class="string">job1</span></span><br><span class="line"></span><br><span class="line"><span class="attr">job3:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">needs:</span> [<span class="string">job1</span>, <span class="string">job2</span>]</span><br></pre></td></tr></table></figure>

这里的`needs`声明了`job2` 必须等待 `job1` 成功完成，`job3`必须等待 `job1` 和 `job2`依次成功完成

每个任务默认超时时间最长为 `360`分钟，你可以通过 `timeout-minutes` 进行配置:

<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">jobs:</span></span><br><span class="line">	<span class="attr">job1:</span></span><br><span class="line">		<span class="attr">timeout-minutes:</span></span><br></pre></td></tr></table></figure>

## runs-on & strategy

`runs-on` 指定了任务的 `runner` 即执行环境，`runner` 分两种：`GitHub-hosted runner` 和 `self-hosted runner` 

所谓的 `self-hosted runner` 就是用你自己的机器，但是需要 `GitHub` 能进行访问并给与其所需的机器权限，这个不在本文描述范围内，有兴趣可参考 `self-hosted runner` 

`GitHub-hosted runner` 其实就是 `GitHub` 提供的虚拟环境，目前有以下四种:

1. `windows-latest : Windows Server 2019`

2. `ubuntu-latest`或 `ubuntu-18.04`

3. `ubuntu-16.04` : `Ubuntu 16.04`

4. `macos-latest` : `macOS Catalina 10.15`

比较常见的:

<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="string">runs-on:ubuntu-latest</span></span><br></pre></td></tr></table></figure>

## runs-on 多环境

有时候我们常常需要对多个操作系统、多个平台、多个编程语言版本进行测试，为此我们可以配置一个构建矩阵

例如：

<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">runs-on:</span> <span class="string">$&#123;&#123;</span> <span class="string">matrix.os</span> <span class="string">&#125;&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="attr">strategy:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">matrix:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">os:</span> [<span class="string">ubuntu-16.04</span>, <span class="string">ubuntu-18.04</span>]</span><br><span class="line"></span><br><span class="line"><span class="attr">node:</span> [<span class="number">6</span>, <span class="number">8</span>, <span class="number">10</span>]</span><br></pre></td></tr></table></figure>

示例中配置了两种`os`操作系统和三种 `node`版本即总共六种情况的构建矩阵， `$&#123;&#123;matrix.os&#125;&#125;</code> 是一个上下文参数</p>
</li>
</ol>
<p><code>strategy</code> 策略，包括：</p>
<ol>
<li><p><code>matrix</code>: 构建矩阵</p>
</li>
<li><p><code>fail-fast</code> : 默认为<code>true</code> ，即一旦某个矩阵任务失败则立即取消所有还在进行中的任务</p>
</li>
<li><p><code>max-paraller</code> : 可同时执行的最大并发数，默认情况下 <code>GitHub</code> 会动态调整</p>
</li>
</ol>
<p>示例：</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">runs-on:</span> <span class="string">$&#123;&#123;</span> <span class="string">matrix.os</span> <span class="string">&#125;&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="attr">strategy:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">matrix:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">os:</span> [<span class="string">macos-latest</span>, <span class="string">windows-latest</span>, <span class="string">ubuntu-18.04</span>]</span><br><span class="line"></span><br><span class="line"><span class="attr">node:</span> [<span class="number">4</span>, <span class="number">6</span>, <span class="number">8</span>, <span class="number">10</span>]</span><br><span class="line"></span><br><span class="line"><span class="attr">include:</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># includes a new variable of npm with a value of 2 for the matrix leg matching the os and version</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">os:</span> <span class="string">windows-latest</span></span><br><span class="line"></span><br><span class="line"><span class="attr">node:</span> <span class="number">4</span></span><br><span class="line"></span><br><span class="line"><span class="attr">npm:</span> <span class="number">2</span></span><br></pre></td></tr></table></figure>

<p><code>include</code>声明了 <code>os</code> 为 <code>windows-latest</code> 时，增加一个 <code>node</code>和<code>npm</code>分别使用特定的版本的矩阵环境</p>
<p>与<code>include</code> 相反的就是<code>exclude</code> ：</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">runs-on:</span> <span class="string">$&#123;&#123;</span> <span class="string">matrix.os</span> <span class="string">&#125;&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="attr">strategy:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">matrix:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">os:</span> [<span class="string">macos-latest</span>, <span class="string">windows-latest</span>, <span class="string">ubuntu-18.04</span>]</span><br><span class="line"></span><br><span class="line"><span class="attr">node:</span> [<span class="number">4</span>, <span class="number">6</span>, <span class="number">8</span>, <span class="number">10</span>]</span><br><span class="line"></span><br><span class="line"><span class="attr">exclude:</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># excludes node 4 on macOS</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">os:</span> <span class="string">macos-latest</span></span><br><span class="line"></span><br><span class="line"><span class="attr">node:</span> <span class="number">4</span></span><br></pre></td></tr></table></figure>

<p><code>exclude</code> 用来删除特定的配置项，比如这里当<code>os</code> 为 <code>macos-latest</code>，将 <code>node</code>为 4 的版本从构建矩阵中移除</p>
<h2 id="steps"><a href="#steps" class="headerlink" title="steps"></a>steps</h2><p><code>steps</code> 的通用格式类似于：</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">steps:</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">&lt;step_name&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="attr">uses:</span> <span class="string">&lt;action&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="attr">with:</span></span><br><span class="line"></span><br><span class="line"><span class="string">&lt;parameter_name&gt;:</span> <span class="string">&lt;parameter_value&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="attr">id:</span> <span class="string">&lt;step_id&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="attr">continue-on-error:</span> <span class="literal">true</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">&lt;step_name&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="attr">timeout-minutes:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">run:</span> <span class="string">&lt;commands&gt;</span></span><br></pre></td></tr></table></figure>

<p>每个 <code>step</code> 步骤可以有:</p>
<ol>
<li><p><code>id</code> : 每个步骤的唯一标识符</p>
</li>
<li><p><code>name</code> : 步骤的名称</p>
</li>
<li><p><code>uses</code> : 使用哪个<code>action</code></p>
</li>
<li><p><code>run</code>: 执行哪些指令</p>
</li>
<li><p><code>with</code>: 指定某个<code>action</code> 可能需要输入的参数</p>
</li>
<li><p><code>continue-on-error</code> : 设置为 <code>true</code> 允许此步骤失败<code>job</code> 仍然通过</p>
</li>
<li><p><code>timeout-minutes : step</code> 的超时时间</p>
</li>
</ol>
<h2 id="action"><a href="#action" class="headerlink" title="action"></a>action</h2><p><code>action</code> 动作通常是可以通用的，这意味着你可以直接使用别人定义好的<code>action</code></p>
<h2 id="checkout-action"><a href="#checkout-action" class="headerlink" title="checkout action"></a>checkout action</h2><p><code>checkout action</code> 是一个标准动作，当以下情况时必须且需要率先使用:</p>
<ol>
<li><p><code>workflow</code> 需要项目库的代码副本，比如构建、测试、或持续集成这些操作</p>
</li>
<li><p><code>workflow</code> 中至少有一个 <code>action</code>是在同一个项目库下定义的</p>
</li>
</ol>
<p>使用示例：</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="bullet">-</span> <span class="string">users:action/checkout@v1</span></span><br></pre></td></tr></table></figure>

<p>如果你只想浅克隆你的库，或者只复制最新的版本，你可以在 <code>with</code>中使用<code>fetch-depth</code>声明，例如:</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="bullet">-</span> <span class="string">user:action/checkout@v1</span></span><br><span class="line">  <span class="attr">with:</span></span><br><span class="line">  	<span class="string">fetch-depth:1</span></span><br></pre></td></tr></table></figure>

<h2 id="引用-action"><a href="#引用-action" class="headerlink" title="引用 action"></a>引用 action</h2><ol>
<li><p>官方<code>action</code>标准库: github.com/actions</p>
</li>
<li><p>社区库:<code>marketplace</code></p>
</li>
</ol>
<h2 id="引用公有库中的-action"><a href="#引用公有库中的-action" class="headerlink" title="引用公有库中的 action"></a>引用公有库中的 action</h2><p>引用 <code>action</code> 的格式是<code>&#123;owner&#125;/&#123;repo&#125;@&#123;ref&#125;</code>或 <code>&#123;owner&#125;/&#123;repo&#125;/&#123;path&#125;@&#123;ref&#125;</code>，例如上例的中<code>actions/checkout@v1</code>，你还可以使用标准库中的其它 <code>action</code>，如设置 <code>node</code> 版本:</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">jobs:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">my_first_job:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">name:</span> <span class="string">My</span> <span class="string">Job</span> <span class="string">Name</span></span><br><span class="line"></span><br><span class="line"><span class="attr">steps:</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">uses:</span> <span class="string">actions/setup-node@v1</span></span><br><span class="line"></span><br><span class="line"><span class="attr">with:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">node-version:</span> <span class="number">10.</span><span class="string">x</span></span><br></pre></td></tr></table></figure>

<h2 id="引用同一个库中的-action"><a href="#引用同一个库中的-action" class="headerlink" title="引用同一个库中的 action"></a>引用同一个库中的 action</h2><p>引用格式：<code>&#123;owner&#125;/&#123;repo&#125;@&#123;ref&#125;</code>或 <code>./path/to/dir</code></p>
<p>例如项目文件结构为：</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="string">--</span> <span class="string">hello-world</span> <span class="string">(repository)</span></span><br><span class="line"></span><br><span class="line"> <span class="string">|__</span> <span class="string">.github</span></span><br><span class="line"></span><br><span class="line"> <span class="string">└──</span> <span class="string">workflows</span></span><br><span class="line"></span><br><span class="line"> <span class="string">└──</span> <span class="string">my-first-workflow.yml</span></span><br><span class="line"></span><br><span class="line"> <span class="string">└──</span> <span class="string">actions</span></span><br><span class="line"></span><br><span class="line"> <span class="string">|__</span> <span class="string">hello-world-action</span></span><br><span class="line"></span><br><span class="line"> <span class="string">└──</span> <span class="string">action.yml</span></span><br></pre></td></tr></table></figure>

<p>当你想要在<code>workflow</code> 中引用自己的<code>action</code> 时可以：</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">jobs:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">build:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">runs-on:</span> <span class="string">ubuntu-latest</span></span><br><span class="line"></span><br><span class="line"><span class="attr">steps:</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># This step checks out a copy of your repository.</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">uses:</span> <span class="string">actions/checkout@v1</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># This step references the directory that contains the action.</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">uses:</span> <span class="string">./.github/actions/hello-world-action</span></span><br></pre></td></tr></table></figure>

<h2 id="引用-Docker-Hub-上的-container"><a href="#引用-Docker-Hub-上的-container" class="headerlink" title="引用 Docker Hub 上的 container"></a>引用 Docker Hub 上的 container</h2><p>如果某个 <code>action</code> 定义在了一个<code>docker container image</code> 中且推送到了<code>Docker Hub</code> 上，你也可以引入它，格式是<code>docker://&#123;image&#125;:&#123;tag&#125;</code> ，示例：</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">jobs:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">my_first_job:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">steps:</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">My</span> <span class="string">first</span> <span class="string">step</span></span><br><span class="line"></span><br><span class="line"><span class="attr">uses:</span> <span class="string">docker://alpine:3.8</span></span><br></pre></td></tr></table></figure>

<p>更多信息参考:<code>Docker-image.yml workflow</code> 和<code>Creating a Docker container ``action</code></p>
<h2 id="构建-actions"><a href="#构建-actions" class="headerlink" title="构建 actions"></a>构建 actions</h2><p>请参考：<code>building-actions</code></p>
<h2 id="env"><a href="#env" class="headerlink" title="env"></a>env</h2><p>环境变量可以配置在以下地方:</p>
<ol>
<li><p><code>env</code></p>
</li>
<li><p><code>jobs.&lt;job_id&gt;.env</code></p>
</li>
<li><p><code>jobs.&lt;job_id&gt;.steps.env</code></p>
</li>
</ol>
<p>示例：</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">env:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">NODE_ENV:</span> <span class="string">dev</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="attr">jobs:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">job1:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">env:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">NODE_ENV:</span> <span class="string">test</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="attr">steps:</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">env:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">NODE_ENV:</span> <span class="string">prod</span></span><br></pre></td></tr></table></figure>

<p>如果重复，优先使用最近的那个</p>
<h2 id="if-amp-context"><a href="#if-amp-context" class="headerlink" title="if &amp; context"></a>if &amp; context</h2><p>你可以在 <code>job</code>和<code>step</code>中使用<code>if</code>条件语句，只有满足条件时才执行具体的<code>job</code> 或 <code>step</code> :</p>
<ol>
<li><p><code>jobs.&lt;job_id&gt;.if</code></p>
</li>
<li><p><code>jobs.&lt;job_id&gt;.steps.if</code></p>
</li>
</ol>
<p>任务状态检查函数:</p>
<ol start="3">
<li><p><code>success()</code> : 当上一步执行成功时返回 <code>true</code></p>
</li>
<li><p><code>always()</code> : 总是返回 <code>true</code></p>
</li>
<li><p><code>cancelled()</code> : 当 <code>workflow</code>被取消时返回 <code>true</code></p>
</li>
<li><p><code>failure()</code> : 当上一步执行失败时返回 <code>true</code></p>
</li>
</ol>
<p>例如：</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">steps:</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">step1</span></span><br><span class="line"></span><br><span class="line"><span class="attr">if:</span> <span class="string">always()</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">step2</span></span><br><span class="line"></span><br><span class="line"><span class="attr">if:</span> <span class="string">success()</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">step3</span></span><br><span class="line"></span><br><span class="line"><span class="attr">if:</span> <span class="string">failure()</span></span><br></pre></td></tr></table></figure>

<p>意思就是 <code>step1</code>总是执行，<code>step2</code> 需要上一步执行成功才执行，<code>step3</code> 只有当上一步执行失败才执行</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="string">$&#123;&#123;&lt;expression&gt;&#125;&#125;</span></span><br></pre></td></tr></table></figure>

<p>上下文和表达式: <code>$&#123;&#123;<expression>&#125;&#125; </code></p>
<p>有时候我们需要与第三方平台进行交互，这时候通常需要配置一个<code>token</code>，但是显然这个 <code>token</code> 不可能明文使用，这种个情况下我们要做的就是：</p>
<ol>
<li>在具体 <code>repository</code> 库<code>Settings</code> 的 <code>Secrets</code> 中添加一个密钥，如 <code>SOMEONE_TOKEN</code></li>
</ol>
<p><code>注：关于如何获取github或者其他平台的密钥及授权不是本文重点，大家可以自行百度</code></p>
<ol start="2">
<li>然后在<code>workflow</code>中就可以通过 <code>$&#123;&#123;secrets.SOMEONE_TOKEN&#125;&#125;</code> 将 <code>token</code>安全地传递给环境变量</li>
</ol>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">steps:</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">My</span> <span class="string">first</span> <span class="string">action</span></span><br><span class="line"></span><br><span class="line"><span class="attr">env:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">SOMEONE_TOKEN:</span> <span class="string">$&#123;&#123;</span> <span class="string">secrets.SOMEONE_TOKEN</span> <span class="string">&#125;&#125;</span></span><br></pre></td></tr></table></figure>

<p>这里的<code>secrets</code> 就是一个上下文，除此之外还有很多，比如：</p>
<ol>
<li><p><code>github.event_name</code> : 触发<code>workflow</code>的事件名称</p>
</li>
<li><p><code>job.status</code> : 当前<code>job</code> 的状态，如 <code>success, failure, or cancelled</code></p>
</li>
<li><p><code>steps.&lt;step id&gt;.outputs</code> : 某个 <code>action</code> 的输出</p>
</li>
<li><p><code>runner.os : runner</code>的操作系统如 <code>Linux, Windows, or macOS</code></p>
</li>
</ol>
<p>这里只列举了少数几个</p>
<p>另外在<code>if</code> 中使用时不需要 <code>$&#123;&#123;&#125;&#125;</code> 符号，比如：</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">steps:</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">My</span> <span class="string">first</span> <span class="string">step</span></span><br><span class="line"></span><br><span class="line"><span class="attr">if:</span> <span class="string">github.event_name</span> <span class="string">==</span> <span class="string">&#x27;pull_request&#x27;</span> <span class="string">&amp;&amp;</span> <span class="string">github.event.action</span> <span class="string">==</span> <span class="string">&#x27;unassigned&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="attr">run:</span> <span class="string">echo</span> <span class="string">This</span> <span class="string">event</span> <span class="string">is</span> <span class="string">a</span> <span class="string">pull</span> <span class="string">request</span> <span class="string">that</span> <span class="string">had</span> <span class="string">an</span> <span class="string">assignee</span> <span class="string">removed.</span></span><br></pre></td></tr></table></figure>

<p>上下文和表达式详细信息请参考：<code>contexts-and-expression</code></p>
<p>看到这里你应该对示例文件有了一定的理解，并且想自己去实验一下了，那就去actions页面找到官方示例去体验一下吧</p>
<p>但对于为什么要备份<code>package.json</code>我想大家还是心存疑惑，那我们看看具体内容吧</p>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;name&quot;</span>: <span class="string">&quot;hexo-site&quot;</span>,</span><br><span class="line">  <span class="attr">&quot;version&quot;</span>: <span class="string">&quot;0.0.0&quot;</span>,</span><br><span class="line">  <span class="attr">&quot;private&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">  <span class="attr">&quot;scripts&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;build&quot;</span>: <span class="string">&quot;hexo generate&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;clean&quot;</span>: <span class="string">&quot;hexo clean&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;deploy&quot;</span>: <span class="string">&quot;hexo deploy&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;server&quot;</span>: <span class="string">&quot;hexo server&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">&quot;hexo&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;version&quot;</span>: <span class="string">&quot;5.2.0&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">&quot;dependencies&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;cheerio&quot;</span>: <span class="string">&quot;^1.0.0-rc.3&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;gulp-htmlmin&quot;</span>: <span class="string">&quot;^5.0.1&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo&quot;</span>: <span class="string">&quot;^5.2.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-baidupush&quot;</span>: <span class="string">&quot;^1.0.2&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-charts&quot;</span>: <span class="string">&quot;^1.0.3&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-cli&quot;</span>: <span class="string">&quot;^4.2.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-deployer-git&quot;</span>: <span class="string">&quot;^2.1.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-generator-archive&quot;</span>: <span class="string">&quot;^1.0.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-generator-baidu-sitemap&quot;</span>: <span class="string">&quot;^0.1.9&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-generator-category&quot;</span>: <span class="string">&quot;^1.0.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-generator-feed&quot;</span>: <span class="string">&quot;^3.0.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-generator-index&quot;</span>: <span class="string">&quot;^2.0.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-generator-search&quot;</span>: <span class="string">&quot;^2.4.1&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-generator-sitemap&quot;</span>: <span class="string">&quot;^2.1.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-generator-tag&quot;</span>: <span class="string">&quot;^1.0.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-render-pug&quot;</span>: <span class="string">&quot;^2.1.4&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-renderer-ejs&quot;</span>: <span class="string">&quot;^1.0.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-renderer-marked&quot;</span>: <span class="string">&quot;^3.3.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-renderer-pug&quot;</span>: <span class="string">&quot;^1.0.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-renderer-stylus&quot;</span>: <span class="string">&quot;^2.0.1&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-server&quot;</span>: <span class="string">&quot;^2.0.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-tag-aplayer&quot;</span>: <span class="string">&quot;^3.0.4&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-wordcount&quot;</span>: <span class="string">&quot;^6.0.1&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;pump&quot;</span>: <span class="string">&quot;^3.0.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;terser&quot;</span>: <span class="string">&quot;^4.8.0&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">&quot;devDependencies&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;gulp&quot;</span>: <span class="string">&quot;4.0.2&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;gulp-cli&quot;</span>: <span class="string">&quot;2.3.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;gulp-clean-css&quot;</span>: <span class="string">&quot;^4.3.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;gulp-htmlclean&quot;</span>: <span class="string">&quot;^2.7.22&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;gulp-uglify&quot;</span>: <span class="string">&quot;^3.0.2&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>我们看到里面包含了npm脚本和一些依赖（包含dev版本的），有了这个文件我们只需执行npm i(install)即可自动安装所以所需依赖插件，而不用一个一个去敲一堆命令啦，理解了这个简单的例子，大家在部署其他项目时也许能更得心应手吧</p>
<p>今天的分享就到这里结束了，本文还有很多纰漏还请读者指出，后续将更正，下篇文章见</p>
</div><ul class="post-copyright"><li class="post-copyright-author"><strong>本文作者：</strong>CC康纳百川</li><li class="post-copyright-link"><strong>本文链接：</strong><a href="https://ccknbc.github.io/yun/posts/9/" title="Github Actions为什么这么香">https://ccknbc.github.io/yun/posts/9/</a></li><li class="post-copyright-license"><strong>版权声明：</strong>本博客所有文章除特别声明外，均默认采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank" rel="noopener" title="CC BY-NC-SA 4.0 "><svg class="icon"><use xlink:href="#icon-creative-commons-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-by-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-nc-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-sa-line"></use></svg></a> 许可协议。</li></ul><script>document.addEventListener('copy', function (event) {
  const clipboardData = event.clipboardData || window.clipboardData;
  if (!clipboardData) { return; }
  const text = window.getSelection().toString();
  if (text) {
    event.preventDefault();
    clipboardData.setData('text/plain', text + '\n\n本文作者：CC康纳百川\n本文链接：https://ccknbc.github.io/yun/posts/9/\n版权声明：本博客所有文章除特别声明外，均默认采用 CC BY-NC-SA 4.0 许可协议。');
  }
});</script></section></article><div class="post-nav"><div class="post-nav-item"><a class="post-nav-prev" href="/yun/posts/10/" rel="prev" title="如何在中国大陆享受JOOX音乐服务"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-left-s-line"></use></svg><span class="post-nav-text">如何在中国大陆享受JOOX音乐服务</span></a></div><div class="post-nav-item"><a class="post-nav-next" href="/yun/posts/8/" rel="next" title="我为什么选择了Tidio做聊天系统"><span class="post-nav-text">我为什么选择了Tidio做聊天系统</span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-right-s-line"></use></svg></a></div></div></div><div id="comment"><div class="comment-tooltip text-center"><span>点击按钮跳转 GitHub Issues 评论</span><br><span>若没有本文 Issue，您可以使用 Comment 模版新建</span><br><a class="hty-button hty-button--raised" id="github-issues" target="_blank" rel="noopener" href="https://github.com/ccknbc-backup/comments/issues?q=is:issue+Github Actions为什么这么香">GitHub Issues</a></div><div id="disqus_thread"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/disqusjs@latest/dist/disqusjs.css"><script src="https://cdn.jsdelivr.net/npm/disqusjs@latest/dist/disqus.js"></script><script>const disqusjsConfig = {"enable":true,"shortname":"ccknbc","count":true,"apikey":"Z6F8Gc9KvW2YCh6p11VRVNvMBYfdaKR3IMRFLVmoYaIQJ2ysp38fZ9tivPdgyBWu","siteName":"CC的部落格","identifier":null,"url":null,"title":null,"nesting":4,"nocomment":"这里冷冷清清的，一条评论也没有，快来留下脚印吧！","admin":"ccknbc","adminLabel":"ccknbc"}
function loadDisqus() {
  const dsqjs = new DisqusJS(disqusjsConfig)
}</script><script src="/yun/js/comments/disqus.js"></script><style>.utterances {
  max-width: 100%;
}</style><script src="https://utteranc.es/client.js" repo="ccknbc-backup/comments" issue-term="title" label="💬comment" theme="github-light" crossorigin="anonymous" async></script></div></main><footer class="sidebar-translate" id="footer"><div class="copyright"><span>&copy; 2020 </span><span class="with-love" id="animate"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-cloud-line"></use></svg></span><span class="author"> CC康纳百川</span></div><div class="powered"><span>由 <a href="https://hexo.io" target="_blank" rel="noopener">Hexo</a> 驱动 v5.2.0</span><span class="footer-separator">|</span><span>主题 - <a rel="noopener" href="https://github.com/YunYouJun/hexo-theme-yun" target="_blank"><span>Yun</span></a> v1.2.0</span></div><div class="live_time"><span>本博客已萌萌哒地运行</span><span id="display_live_time"></span><span class="moe-text">(●'◡'●)</span><script>function blog_live_time() {
  window.setTimeout(blog_live_time, 1000);
  const start = new Date('2020-06-04T00:00:00');
  const now = new Date();
  const timeDiff = (now.getTime() - start.getTime());
  const msPerMinute = 60 * 1000;
  const msPerHour = 60 * msPerMinute;
  const msPerDay = 24 * msPerHour;
  const passDay = Math.floor(timeDiff / msPerDay);
  const passHour = Math.floor((timeDiff % msPerDay) / 60 / 60 / 1000);
  const passMinute = Math.floor((timeDiff % msPerHour) / 60 / 1000);
  const passSecond = Math.floor((timeDiff % msPerMinute) / 1000);
  display_live_time.innerHTML = " " + passDay + " 天 " + passHour + " 小时 " + passMinute + " 分 " + passSecond + " 秒";
}
blog_live_time();
</script></div><div id="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_site_uv" title="总访客量"><span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-user-line"></use></svg></span><span id="busuanzi_value_site_uv"></span></span><span class="footer-separator">|</span><span id="busuanzi_container_site_pv" title="总访问量"><span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-eye-line"></use></svg></span><span id="busuanzi_value_site_pv"></span></span></div></footer><a class="hty-icon-button" id="goUp" aria-label="back-to-top" href="#"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-up-s-line"></use></svg><svg class="progress-circle-container" viewBox="0 0 100 100"><circle class="progress-circle" id="progressCircle" cx="50" cy="50" r="48" fill="none" stroke="#0078E7" stroke-width="2" stroke-linecap="round"></circle></svg></a><a class="popup-trigger hty-icon-button icon-search" id="search" href="javascript:;" title="搜索"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-search-line"></use></svg></span></a><script>window.addEventListener("DOMContentLoaded", () => {
  // Handle and trigger popup window
  document.querySelector(".popup-trigger").addEventListener("click", () => {
    document.querySelector(".popup").classList.add("show");
    setTimeout(() => {
      document.querySelector(".search-input").focus();
    }, 100);
  });

  // Monitor main search box
  const onPopupClose = () => {
    document.querySelector(".popup").classList.remove("show");
  };

  document.querySelector(".popup-btn-close").addEventListener("click", () => {
    onPopupClose();
  });

  window.addEventListener("keyup", event => {
    if (event.key === "Escape") {
      onPopupClose();
    }
  });
});
</script><script src="/yun/js/search/local-search.js" defer></script><div class="popup search-popup"><div class="search-header"><span class="popup-btn-close close-icon hty-icon-button"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-close-line"></use></svg></span></div><div class="search-input-container"><input class="search-input" id="local-search-input" type="text" placeholder="搜索..." value=""></div><div id="local-search-result"></div></div><script>const date = new Date();
const today = (date.getMonth() + 1) + "-" + date.getDate()
const mourn_days = ["4-4","9-18"]
if (mourn_days.includes(today)) {
  document.documentElement.style.filter = "grayscale(1)";
}</script></div><script defer src="/yun/js/utils.js"></script><script defer src="/yun/js/hexo-theme-yun.js"></script></body></html>